package org.algorithm.binarysearchrecursion;

public class NoRecursionSearch {

    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5, 6, 7, 9};
        System.out.println(search(4, arr));
    }

    public static int search(int target, int[] arr) {
        int l = 0;
        int r = arr.length - 1;
        while (l <= r) {
            //二分查找和非递归二分查找都需要使用这个二分
            int mid = (l + r) / 2;
            if (arr[mid] == target) {
                return mid;
            } else {
                if (arr[mid] > target) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }
        }
        return -1;
    }
}
